package com.ruoyi.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.entity.Project;
import com.ruoyi.common.entity.ProjectItem;
import com.ruoyi.common.mapper.ProjectItemsMapper;
import com.ruoyi.common.mapper.ProjectMapper;
import com.ruoyi.common.service.IProjectItemsService;
import com.ruoyi.common.utils.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-17
 */
@Service
public class ProjectItemsServiceImpl extends ServiceImpl<ProjectItemsMapper, ProjectItem> implements IProjectItemsService
{
    @Autowired
    private ProjectItemsMapper projectItemsMapper;

    /**
     * 查询课程目录
     * 
     * @param id 课程目录主键
     * @return 课程目录
     */
    @Override
    public ProjectItem selectProjectItemsById(Long id)
    {
        return projectItemsMapper.selectProjectItemsById(id);
    }

    /**
     * 查询课程目录列表
     * 
     * @param projectItems 课程目录
     * @return 课程目录
     */
    @Override
    public List<ProjectItem> selectProjectItemsList(ProjectItem projectItems)
    {
        return projectItemsMapper.selectProjectItemsList(projectItems);
    }

    /**
     * 新增课程目录
     * 
     * @param projectItems 课程目录
     * @return 结果
     */
    @Override
    public int insertProjectItems(ProjectItem projectItems)
    {
        projectItems.setCreateTime(LocalDate.now());
        return projectItemsMapper.insertProjectItems(projectItems);
    }

    /**
     * 修改课程目录
     * 
     * @param projectItems 课程目录
     * @return 结果
     */
    @Override
    public String updateProjectItems(ProjectItem projectItems)
    {
        projectItems.setUpdateTime(LocalDate.now());
        int i = projectItemsMapper.updateProjectItems(projectItems);
        if (i > 0) {
            return "修改成功";
        }
        return "修改失败";

    }

    /**
     * 批量删除课程目录
     * 
     * @param ids 需要删除的课程目录主键
     * @return 结果
     */
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    @Transactional
    public int deleteProjectItemsByIds(Long[] ids)
    {
        /*for (Long id : ids) {
            ProjectItem projectItem = projectItemsMapper.selectOne(Wrappers.<ProjectItem>lambdaQuery()
                    .eq(ProjectItem::getProjectId, id));
            Project project = projectMapper.selectOne(Wrappers.<Project>lambdaQuery()
                    .eq(Project::getId, projectItem.getProjectId()));
            if (ObjectUtil.isNotNull(project)) {
                throw new RuntimeException("该项目下存在，描述不可删除");
            }

        }*/
        List<Long> projectIds = new ArrayList<>();
        for (Long id : ids) {
            ProjectItem projectItem = projectItemsMapper.selectOne(
                    Wrappers.<ProjectItem>lambdaQuery()
                            .eq(ProjectItem::getProjectId, id)
                            .select(ProjectItem::getProjectId));
            projectIds.add(projectItem.getProjectId());
        }

        projectMapper.deleteBatchIds(projectIds);
        return projectItemsMapper.deleteProjectItemsByIds(ids);
    }

    /**
     * 删除课程目录信息
     * 
     * @param id 课程目录主键
     * @return 结果
     */
    @Override
    public int deleteProjectItemsById(Long id)
    {
        return projectItemsMapper.deleteProjectItemsById(id);
    }

    @Override
    public void insertSpiderProjectItems(List<ProjectItem> projectItems) {

    }


}
